package com.dianrui.moonfire.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.adapter.WalletListMainAdapter;
import com.dianrui.moonfire.bean.WalletListBean;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.JsonUtils;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyOrderListMainActivity extends BaseActivity {
    private List<WalletListBean> mWalletBeanList = new ArrayList<>();
    private WalletListMainAdapter walletListMainAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @Override
    public int getContentViewId() {
        return R.layout.my_orderlist_main;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.my_txt1));
        getMemberTrip();
        LinearLayoutManager layoutManager = new LinearLayoutManager(MyOrderListMainActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(MyOrderListMainActivity.this, LinearLayoutManager.VERTICAL));
        walletListMainAdapter = new WalletListMainAdapter(mWalletBeanList, MyOrderListMainActivity.this);
        recyclerView.setAdapter(walletListMainAdapter);
        walletListMainAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WalletListBean walletListBean= (WalletListBean) adapter.getItem(position);
                if (walletListBean != null) {
                    if (position == 0) {
                        JumpActivitys(MyOrderActivity.class);
                    }else if (position == 1) {
                        Intent intent=new Intent(MyOrderListMainActivity.this,MyOrderBabyActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("type_id",walletListBean.type_id);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }


            }
        });
    }

    private void getMemberTrip() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        XHttpRequest.getInstance().postRequests(Url.BABYTRIPLIST, jsonObject.toString(), new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        if (isFinishing()) {
                            return;
                        }
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                String data= JsonUtils.getJsonStr(result,"data");
                                if (!StringUtils.isEmpty(data)) {
                                    List<WalletListBean> list=JsonUtils.jsonStrToList(data, new TypeToken<ArrayList<WalletListBean>>(){}.getType());
                                    if (list != null && list.size()>0) {
                                        mWalletBeanList.addAll(list);
                                        walletListMainAdapter.setNewData(list);
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public void initData() {}

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
